<?php
class MailboxComponent extends Component{
    
    private $user = 'ems.ariel@dotcomlb.com';
    private $password = 'dotcom2013';
    private $attachmentsDir;
    private $host_name = '{imap.gmail.com:993/imap/ssl}INBOX';
    private $inbox;
    public function test(){
        //header ('Content-Type: text/plain; charset=UTF-8');
        /* grab emails */
        $emails = $this->get_mails('ALL');
        pr($emails);die;
        rsort($emails);
        $email = $emails[0];
        //$result = array();
        //foreach($emails as $email_number) {
            $result = $this->get_mail($email);
            //$result[]=$arr;
        //}
        pr($result);die;
    }
    public function get_mails($criteria ="All"){
        $emails = imap_search($this->inbox,$criteria);
        return $emails;
    }
    public function get_mail($email_id){
        $overview = $this->get_header($email_id);
        $message = $this->get_body($email_id);
        $arr = array();
        //$arr['status']=$overview[0]->seen ? 'read' : 'unread';
        //$arr['number_email'] = $email_number;
        $arr['subject']=$overview['subject'];
        $arr['from']['personal']=$overview['from']['personal'];
        $arr['from']['address'] = $overview['from']['address'];
        $arr['date'] = $overview['date'];
        $arr['message']=$message;
        return $arr;
    }
    public function __construct() {
        $this->set_attachment_dir();
        $this->inbox =imap_open($this->host_name, $this->user, $this->password);
    }
    private function set_attachment_dir(){
        $this->attachmentsDir = WWW_ROOT.'files'.'/attachments';
    }
    private function get_header($number){
        $overview = @imap_headerinfo($this->inbox,$number,0);
        $return = array();
        $return['date']=$overview->date;
        $return['subject']=  $this->convent_utf_8($overview->subject);
        //$tmpfrom = imap_mime_header_decode($overview->from[0]->personal);
        //$from = quoted_printable_decode($tmpfrom[0]->text);
        //echo $this->replace_imap($overview->from[0]->personal,0,"UTF-8");die;
        $return['from']['personal']=  $this->convent_utf_8($overview->from[0]->personal);
        $return['from']['address']=$overview->from[0]->mailbox.'@'.$overview->from[0]->host;
        return $return;
    }
    private function convent_utf_8($str){
        $str = strtolower($str);
        $str = utf8_decode(imap_utf8($str));
        $str = strtolower($str);
        $str = ucfirst($str);
        return $str;
    }
    private function get_body($number){
        // GET HTML BODY
        $dataHtml = $this->get_part($this->inbox, $number, "TEXT/HTML");
        $msgBody = '';
        if($dataHtml!=""){
            $msgBody = preg_replace("/<div[^>]*class=\"gmail_quote\">(.*?)<\\/div>/si", "", $dataHtml);
            $msgBody = preg_replace("/<font[^>]*>(.*?)<\\/font>/si", "", $msgBody);
            $msgBody= strip_tags($msgBody,'<div>') ;
            $msgBody = preg_replace("/<div><\\/div>/si", "", $msgBody);
            $msgBody = str_replace("--", "", $msgBody);
            $msgBody=$this->convent_utf_8($msgBody);
            //$msgBody = preg_replace("/<br[^>]*clear=\"all\">(.*?)<br>/si","",$msgBody);
        }
        return $msgBody;
    }
    private  function get_mime_type(&$structure) {
        $primary_mime_type = array("TEXT", "MULTIPART","MESSAGE", "APPLICATION", "AUDIO","IMAGE", "VIDEO", "OTHER");
        if($structure->subtype) {
            return $primary_mime_type[(int) $structure->type] . '/' .$structure->subtype;
        }
        return "TEXT/PLAIN";
   }
   private  function get_part($stream, $msg_number, $mime_type, $structure = false,$part_number = false) {
   	if(!$structure) {
            $structure = imap_fetchstructure($stream, $msg_number);
   	}
   	if($structure) {
            if($mime_type == $this->get_mime_type($structure)) {
                    if(!$part_number) {
                            $part_number = "1";
                    }
                    $text = imap_fetchbody($stream, $msg_number, $part_number);
                    if($structure->encoding == 3) {
                            return imap_base64($text);
                    } else if($structure->encoding == 4) {
                            return imap_qprint($text);
                    } else {
                    return $text;
                }
            }
   
            if($structure->type == 1){
                while(list($index, $sub_structure) = each($structure->parts)) {
                    $prefix='';
                    if($part_number) {
                            $prefix = $part_number . '.';
                    }
                    $data = $this->get_part($stream, $msg_number, $mime_type, $sub_structure,$prefix .    ($index + 1));
                    if($data) {
                            return $data;
                    }
                }
            }
        }
   	return false;
    }
}